package com.qingzhu.service.impl;

import com.qingzhu.dao.OrderDao;
import com.qingzhu.dao.UserDao;
import com.qingzhu.dao.impl.OrderDaoImpl;
import com.qingzhu.dao.impl.UserDaoImpl;
import com.qingzhu.pojo.Address;
import com.qingzhu.pojo.User;
import com.qingzhu.pojovo.UserVo;
import com.qingzhu.service.AddressService;
import com.qingzhu.service.UserService;

public class UserServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();
    AddressService addressService = new AddressServiceImpl();
    OrderDao orderDao = new OrderDaoImpl();

    @Override
    public UserVo findById(int id) {
        User user = userDao.findById(id);
        // 查询用户的居住地和家乡地址
        Address defaultAddress = addressService.findDefaultAddress(user.getAddressUrl());
        Address defaultHometown = addressService.findDefaultAddress(user.getHometownUrl());
        user.setAddress(defaultAddress);
        user.setHometown(defaultHometown);
        UserVo userVo = findOrderNum(id);
        userVo.setUser(user);
        return userVo;
    }

    @Override
    public int updateById(User user) {
        return userDao.updateById(user);
    }


    // 根据用户id查询订单数量
    private UserVo findOrderNum(Integer userId){
        UserVo userVo = new UserVo();
        int noPayOrderNum = orderDao.findNoPayOrderNum(userId);
        int noRecGoodsOrderNum = orderDao.findNoRecGoodsOrderNum(userId);
        int noEvaluateOrderNum = orderDao.findNoEvaluateOrderNum(userId);
        userVo.setNoPayOrderNum(noPayOrderNum);
        userVo.setNoRecGoodsOrderNum(noRecGoodsOrderNum);
        userVo.setNoEvaluateOrderNum(noEvaluateOrderNum);
        return userVo;
    };
}
